cmake_minimum_required(VERSION 3.5)
project(yolo_deep_sort)

# Default to C99
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 99)
endif()

# Default to C++17
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Ofast -DNDEBUG -Wno-deprecated-declarations")
# set(OpenCV_DIR /usr/local/OpenCV470/lib/cmake/opencv4)
find_package(OpenCV 4.2 REQUIRED)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_INSTALL_RPATH "/usr/local/OpenCV470/lib")

# 使用正确版本的CUDA
# tensorrt
set(TENSORRT_INCLUDE_DIRS /usr/include/aarch64-linux-gnu/)   # 头文件夹
set(TENSORRT_LIBS /usr/lib/aarch64-linux-gnu/libnvinfer.so /usr/lib/aarch64-linux-gnu/libnvonnxparser.so)  # 依赖动态库
message("tensorrt path: " ${TENSORRT_INCLUDE_DIRS})

# cuda
set(CUDA_ROOT_PATH /usr/local/cuda-11.4/) # 根目录
set(CUDA_INCLUDE_DIRS ${CUDA_ROOT_PATH}/include/)   # 头文件夹
set(CUDA_LIBS ${CUDA_ROOT_PATH}lib64/libcudart.so)  # 依赖动态库

# set(ONNX_RUNTIME_DIR "/home/nvidia/hil_simulate/src/yolo_deep_sort/onnxruntime-linux-arm-1.20.2")
# include_directories(ONNX_RUNTIME_DIR/include)
# link_directories(ONNX_RUNTIME_DIR/lib)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# Find dependencies
find_package(ament_cmake REQUIRED)
find_package(cv_bridge REQUIRED)
find_package(yolo_detect_message REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(std_msgs REQUIRED)
find_package(image_transport REQUIRED)
find_package(Eigen3 REQUIRED)  

# Include directories
include_directories(${CMAKE_SOURCE_DIR}/include
  ${TENSORRT_INCLUDE_DIRS}
  ${CUDA_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
  ${EIGEN3_INCLUDE_DIRS}  
)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

# Define the library
add_library(Deep_Sort 
  # src/FeatureTensor.cpp
  src/hungarianoper.cpp
  src/kalmanfilter.cpp
  src/linear_assignment.cpp
  src/model.cpp
  src/munkres.cpp
  src/nn_matching.cpp
  src/track.cpp
  src/tracker.cpp
  src/yolo_deep_sort_node.cpp
  src/yolo_deep_sort.cpp
  src/yolo_tracker.cpp
)

# Link libraries
target_link_libraries(Deep_Sort 
  ${OpenCV_LIBS} 
  ${TENSORRT_LIBS} 
  ${CUDA_LIBS}
  ${EIGEN3_LIBRARIES}  # 使用 ${EIGEN3_LIBRARIES}
)

ament_target_dependencies(Deep_Sort
  cv_bridge
  yolo_detect_message
  rclcpp
  sensor_msgs
  std_msgs
  image_transport
)

# Define the executable
add_executable(yolo_deep_sort_node
  src/yolo_deep_sort_node.cpp)

# target_link_libraries(yolo_deep_sort_node Deep_Sort ${ONNX_RUNTIME_DIR}/lib/libonnxruntime.so)
target_link_libraries(yolo_deep_sort_node Deep_Sort)

ament_target_dependencies(yolo_deep_sort_node 
  cv_bridge
  yolo_detect_message
  rclcpp
  sensor_msgs
  std_msgs
  image_transport
)

# Install rules
install(TARGETS yolo_deep_sort_node Deep_Sort
  RUNTIME DESTINATION lib/${PROJECT_NAME}
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
)

install(DIRECTORY launch config DESTINATION share/${PROJECT_NAME})

# Generate package environment
ament_package()
